﻿using Simple.Tools;
using SimpleSaas.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SimpleSaas.Web.Common.redis
/// <summary>
/// 版 本 钢易智企2.0 V 2.0.0 钢易智企2.0
/// Copyright (c) 2023 杭州钢易科技有限公司
/// 创建人：曾东明
/// 日 期：2024
/// 描 述：
/// </summary>
{
	public class RedisClientService : IRedisClientService
	{
		private const string KeySeparator = ":";
		public RedisClientService(RedisStoreOption redisStoreOption)
		{
			CSRedis.CSRedisClient csredis;
			if (redisStoreOption.RedisConnectionStrings.Count == 1)
			{
				//普通模式
				csredis = new CSRedis.CSRedisClient(redisStoreOption.RedisConnectionStrings[0]);
			}
			else
			{
				//集群模式
				//实现思路：根据key.GetHashCode() % 节点总数量，确定连向的节点
				//也可以自定义规则(第一个参数设置)
				csredis = new CSRedis.CSRedisClient(null, redisStoreOption.RedisConnectionStrings.ToArray());
			}
			//初始化 RedisHelper
			RedisHelper.Initialization(csredis);
		}

		public async Task<T> GetAsync<T>(string key)
		{
			var result = await RedisHelper.GetAsync<T>(key);
			return result;
		}
		public async Task SetAsync<T>(string key, T item, TimeSpan expiration)
		{
			await RedisHelper.SetAsync(key, item, (int)expiration.TotalSeconds);
		}
		public async Task SetAsync<T>(string key, T item)
		{
			await RedisHelper.SetAsync(key, item);
		}
		public async Task SetAsync<T>(string key, T item, int seconds)
		{
			await RedisHelper.SetAsync(key, item, seconds);
		}
		public async Task DelAsync(string key)
		{
			await RedisHelper.DelAsync(key);
		}
		public async Task SetHashAsync(string key, string field, object value)
		{
			await RedisHelper.HSetAsync(key, field, value);
		}
		public async Task DelHashAsnyc(string key, string[] fields)
		{
			await RedisHelper.HDelAsync(key, fields);
		}
		public async Task<T> GetHashAsync<T>(string key, string field)
		{
			return await RedisHelper.HGetAsync<T>(key, field);
		}

		public async Task<bool> ExpireAsync(string key, int seconds)
		{
			return await RedisHelper.ExpireAsync(key, seconds);
		}
		public async Task<bool> HashExsitAsync(string key, string field)
		{
			return await RedisHelper.HExistsAsync(key, field);
		}
		public async Task<Dictionary<string, T>> GetHashAllAsync<T>(string key)
		{
			return await RedisHelper.HGetAllAsync<T>(key);
		}
		public async Task<int> HIncrAsync(string key, string field, int num)
		{
			return (int)await RedisHelper.HIncrByAsync(key, field, num);
		}
		public async Task<string[]> GetHKeys(string key)
		{
			return await RedisHelper.HKeysAsync(key);
		}

		public async Task<long> GetHLenAsync(string key)
		{

			return await RedisHelper.HLenAsync(key);
		}

		public async Task<long> ListLPushAsync<T>(string key, T[] ts)
		{
			return await RedisHelper.LPushAsync<T>(key, ts);
		}

		public async Task<long> ListLPushAsync<T>(string key, T t)
		{
			return await RedisHelper.LPushAsync(key, t);
		}

		public async Task<long> ListRPushAsync<T>(string key, T[] ts)
		{
			return await RedisHelper.RPushAsync<T>(key, ts);
		}

		public async Task<long> ListRPushAsync<T>(string key, T t)
		{
			return await RedisHelper.RPushAsync<T>(key, t);
		}

		public async Task<T> ListLPopAsync<T>(string key)
		{
			return await RedisHelper.LPopAsync<T>(key);
		}

		public async Task<T> ListRPopAsync<T>(string key)
		{
			return await RedisHelper.RPopAsync<T>(key);
		}

		public async Task<T[]> ListRangeAsync<T>(string key, long start, long end)
		{
			return await RedisHelper.LRangeAsync<T>(key, start, end);
		}
		public long ListPush<T>(string key, T t)
		{
			return RedisHelper.LPush<T>(key, t);
		}

		public async Task<long> ListLenAsync(string key)
		{
			return await RedisHelper.LLenAsync(key);
		}

		public async Task<long> ListRemAsync(string key, int count, object o)
		{
			return await RedisHelper.LRemAsync(key, count, o);
		}
		/// <summary>
		/// 在list 中分页查找list 列表
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="key"></param>
		/// <param name="Params"></param>
		/// <returns></returns>
		public async Task<PageList<T>> ListRangeAsync<T>(string key, BaseParameter Params)
		{
			int count = (int)await RedisHelper.LLenAsync(key);
			return PageList<T>.Create(await RedisHelper.LRangeAsync<T>(key, Params.PageSize * (Params.PageIndex - 1), Params.PageSize * (Params.PageIndex)), Params, count);
		}
		public async Task<int> IncrAsync(string key, TimeSpan time, long value = 1)
		{
			await RedisHelper.ExpireAsync(key, time);
			return (int)(await RedisHelper.IncrByAsync(key, value));
		}
		public async Task<bool> ExistsAsync(string key)
		{
			return await RedisHelper.ExistsAsync(key);
		}
	}
}